package com.naiterui.ehp.bs.doctor.controller.inner;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.doctor.service.ISuperviseService;
import com.naiterui.ehp.bs.doctor.utils.PropertyValueConstants;
import com.naiterui.ehp.bs.doctor.utils.RedisUtils;
import com.naiterui.ehp.bs.doctor.vo.*;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.Api;

/**
 * <pre>
 * 供监管平台对接内部接口
 * </pre>
 *
 * @author fangguanhong
 * @date 2021/1/13 下午1:41
 * @since 1.0.0
 */
@RestController
@RequestMapping("ad/inner/supervise")
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.INNER_API + SwaggerTag.SUPERVISION})
public class AdSuperviseController {

    @Autowired
    private ISuperviseService superviseService;

    @PostMapping("/session/list")
    public ResponseVO<List<SuperviseSessionVO>> getSessionInfo(@RequestBody List<String> sessionIds) {
        return PublicService.returnResponseVO(superviseService.getSessions(sessionIds));
    }

    @GetMapping("/doctor/list")
    public ResponseVO<List<SuperviseDoctorVO>> getDoctorInfo(String start, String end, Long doctorId) {
        return PublicService.returnResponseVO(superviseService.doctorList(start, end, doctorId));
    }

    @GetMapping("prescription")
    public ResponseVO<PrescriptionVO> getPrescription(String sessionId) {
        return PublicService.returnResponseVO(superviseService.getPrescription(sessionId));
    }

    @GetMapping("token/get")
    public ResponseVO<SuperviseAccessTokenVO> getSuperviseAccessToken() {
        return PublicService.returnResponseVO(SuperviseAccessTokenVO.builder()
                                                                    .accessToken(RedisUtils.getString(PropertyValueConstants.SUPERVISE_ACCESS_TOKEN_KEY))
                                                                    .build());
    }

    @GetMapping("token/set")
    public ResponseVO<Void> setSuperviseAccessToken(String accessToken) {
        RedisUtils.set(PropertyValueConstants.SUPERVISE_ACCESS_TOKEN_KEY, accessToken, PropertyValueConstants.SUPER_ACCESS_TOKEN_EXPIRE);
        return PublicService.returnResponseVO(null);
    }

    @GetMapping("/department/list")
    public ResponseVO<List<SuperviseDepartmentVO>> getDepartmentList(String start, String end) {
        return PublicService.returnResponseVO(superviseService.getDepartmentList(start, end));
    }

    @GetMapping("doctor/record/list")
    public ResponseVO<List<SuperviseDoctorRecordVO>> doctorRecordList(String startDate, String endDate, Long doctorId) {
        DateTime start = DateUtil.parseDateTime(startDate);
        DateTime end = DateUtil.parseDateTime(endDate);
        return PublicService.returnResponseVO(superviseService.doctorRecordList(start, end, doctorId));
    }

    @GetMapping("tianjin/sessions")
    public ResponseVO<List<TianjinSuperviseSessionVO>> tianjinSessionInfo(String start, String end)
        throws BusinessException {
        return PublicService.returnResponseVO(superviseService.tianjinSessions(start, end));
    }

    @GetMapping("tianjin/video/consult")
    public ResponseVO<List<TianjinSuperviseSessionVO>> tianjinVideoConsult(String start, String end)
        throws BusinessException {
        return PublicService.returnResponseVO(superviseService.tianjinVideoConsult(start, end));
    }

    @GetMapping("consult/comment")
    public ResponseVO<List<SuperviseConsultCommentVO>> consultComment(String startDate, String endDate) {
        Date start = DateUtil.parseDateTime(startDate).toJdkDate();
        Date end = DateUtil.parseDateTime(endDate).toJdkDate();
        return PublicService.returnResponseVO(superviseService.consultComment(start, end));
    }
}
